/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.Seguridad.Vista;

import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.ArrayList;
import javax.swing.JSpinner.DefaultEditor;
import pe.edu.pucp.Modelo.Configuracion;
import pe.edu.pucp.Modelo.Impuesto;
import pe.edu.pucp.Modelo.Permiso;
import pe.edu.pucp.Util.Constante;
import pe.edu.pucp.Util.ValHelper;

public class ConfiguracionGUI extends javax.swing.JPanel {

    public ConfiguracionGUI() {
        initComponents();           
        ((DefaultEditor) spnCarAlf.getEditor()).getTextField().setEditable(false);
        ((DefaultEditor) spnCarEsp.getEditor()).getTextField().setEditable(false);
        ((DefaultEditor) spnCarNum.getEditor()).getTextField().setEditable(false);

    }

    public void setFormConfiguracion(Configuracion c, Impuesto p){
        if (c!=null){            
            spnCarEsp.setValue(c.getNumcaresp());
            spnCarNum.setValue(c.getNumcarnum());
            spnCarAlf.setValue(c.getNumcaralf());          
            txtIva.setText(String.valueOf(p.getIva()*100));
            txtTarifa.setText(String.valueOf(c.getTarifadist()));
            txtFechMod.setText(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(c.getFechamod()));
        }
    }    
    
    public int getLong(){
        return getCarEsp()+getCarNum()+getCarAlf();
    }       
    
    public int getCarEsp(){
            return Integer.parseInt(spnCarEsp.getValue().toString());
    }

    public int getCarNum(){
        return Integer.parseInt(spnCarNum.getValue().toString());         
    }
    
    public int getCarAlf(){
        return Integer.parseInt(spnCarAlf.getValue().toString());
    }    
    
    public double getIva(){
        return Double.parseDouble(txtIva.getText())/100;
    }

    public double getTarifa(){
        return Double.parseDouble(txtTarifa.getText());
    }      
    
    public void mostrarPermisos(ArrayList<Permiso> list){            
            if (!list.isEmpty()){                                
                btnGuardar.setEnabled(buscarPermiso(list,Constante.P_SEG_CON_GUARDAR)==1);
            }
    }    
  
    public int buscarPermiso(ArrayList<Permiso> list, int idel){
        int per=-1;
        for (Permiso c : list){
            if (c.getElemento().getIdelemento()==idel){ per =c.getPermiso(); }
        }
        return per;
    }     
       
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */

    public void LimpiarDatos(){
        spnCarEsp.setValue(0);
        spnCarNum.setValue(0);
        spnCarAlf.setValue(0);
        txtIva.setText("");
        txtTarifa.setText("");           
    }    
    
    public void addLimpiarListener(ActionListener ml){
        this.btnLimpiar.addActionListener(ml);
    }
        
    public void addGuardarListener(ActionListener ml){
        this.btnGuardar.addActionListener(ml);
    }    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        spnCarNum = new javax.swing.JSpinner();
        spnCarEsp = new javax.swing.JSpinner();
        spnCarAlf = new javax.swing.JSpinner();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        btnLimpiar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        lblIgv = new javax.swing.JLabel();
        lblTarifa = new javax.swing.JLabel();
        txtTarifa = new javax.swing.JTextField();
        txtIva = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        lblTCont1 = new javax.swing.JLabel();
        txtFechMod = new javax.swing.JTextField();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        setMaximumSize(new java.awt.Dimension(764, 700));
        setMinimumSize(new java.awt.Dimension(764, 700));
        setPreferredSize(new java.awt.Dimension(764, 700));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Contraseña", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 15))); // NOI18N

        spnCarNum.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        spnCarEsp.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        spnCarAlf.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        jLabel9.setFont(new java.awt.Font("Ubuntu", 0, 15)); // NOI18N
        jLabel9.setForeground(java.awt.Color.gray);
        jLabel9.setText("Caracteres Numéricos  ");

        jLabel10.setFont(new java.awt.Font("Ubuntu", 0, 15)); // NOI18N
        jLabel10.setForeground(java.awt.Color.gray);
        jLabel10.setText("Caracteres Especiales  ");

        jLabel12.setFont(new java.awt.Font("Ubuntu", 0, 15)); // NOI18N
        jLabel12.setForeground(java.awt.Color.gray);
        jLabel12.setText("Caracteres Alfabeticos ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(jLabel12))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(spnCarAlf, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spnCarEsp, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spnCarNum, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(465, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel9)
                    .addComponent(spnCarNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(spnCarEsp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(spnCarAlf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/refresh.png"))); // NOI18N
        btnLimpiar.setToolTipText("Limpiar");

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/checkmark.png"))); // NOI18N
        btnGuardar.setToolTipText("Guardar");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Envíos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 15))); // NOI18N

        lblIgv.setFont(new java.awt.Font("Ubuntu", 0, 15)); // NOI18N
        lblIgv.setForeground(java.awt.Color.gray);
        lblIgv.setText("IGV");
        lblIgv.setToolTipText("%");

        lblTarifa.setFont(new java.awt.Font("Ubuntu", 0, 15)); // NOI18N
        lblTarifa.setForeground(java.awt.Color.gray);
        lblTarifa.setText("Tarifa");
        lblTarifa.setToolTipText("dólares");

        txtTarifa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtTarifaKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblIgv)
                .addGap(18, 18, 18)
                .addComponent(txtIva, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24)
                .addComponent(lblTarifa)
                .addGap(18, 18, 18)
                .addComponent(txtTarifa, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(429, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(txtTarifa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTarifa)
                    .addComponent(lblIgv)
                    .addComponent(txtIva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 15))); // NOI18N

        lblTCont1.setFont(new java.awt.Font("Ubuntu", 0, 15)); // NOI18N
        lblTCont1.setForeground(java.awt.Color.gray);
        lblTCont1.setText("Ultima Modificacion");
        lblTCont1.setToolTipText("horas");

        txtFechMod.setEnabled(false);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTCont1)
                .addGap(18, 18, 18)
                .addComponent(txtFechMod, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lblTCont1)
                    .addComponent(txtFechMod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(356, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtTarifaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTarifaKeyTyped
        String car = txtTarifa.getText();
        if(car.length()>=6) {
            evt.consume();
        }          
        ValHelper.soloDigitoPunto(evt);
        ValHelper.caracterUnico(evt,txtTarifa.getText(),'.');
    }//GEN-LAST:event_txtTarifaKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JLabel lblIgv;
    private javax.swing.JLabel lblTCont1;
    private javax.swing.JLabel lblTarifa;
    private javax.swing.JSpinner spnCarAlf;
    private javax.swing.JSpinner spnCarEsp;
    private javax.swing.JSpinner spnCarNum;
    private javax.swing.JTextField txtFechMod;
    private javax.swing.JTextField txtIva;
    private javax.swing.JTextField txtTarifa;
    // End of variables declaration//GEN-END:variables
}
